﻿Imports SISSAT.BusinessLayer.DomainModels
Imports SISSAT.BusinessLayer.Services

Namespace Views
    Public Class CourseDetailControlPresenter

        Private Property View As ICourseDetailControlView
        Private Property Service As ICourseService

        Public Sub New(view As ICourseDetailControlView)
            Service = New CourseService
            Me.View = view
            AddHandler Me.View.OnClear, AddressOf OnClear
            AddHandler Me.View.OnInitialization, AddressOf OnInitialization
            AddHandler Me.View.OnSubmit, AddressOf OnSubmit
        End Sub

        Private Sub OnInitialization()
            If Me.View.CourseId = 0 Then
                Return
            End If

            Dim entity As Course = Service.ReadById(Me.View.CourseId)
            Me.View.Code = entity.Code
            Me.View.Description = entity.Description
            Me.View.Name = entity.Name
        End Sub

        Private Sub OnClear()
            Me.View.Code = String.Empty
            Me.View.Description = String.Empty
            Me.View.Name = String.Empty

            OnInitialization()
        End Sub

        Private Sub OnSubmit()
            If Me.View.CourseId = 0 Then
                Service.Create(Me.View.Name, Me.View.Description, Me.View.Code, Me.View.CurrentUserId)
            End If

            Dim entity As Course = Service.ReadById(Me.View.CourseId)
            entity.Code = Me.View.Code
            entity.Description = Me.View.Description
            entity.Name = Me.View.Name
            Service.Edit(entity, Me.View.CurrentUserId)
        End Sub

    End Class
End Namespace

